package demo4;
import java.util.Stack;
class MinStack {
    Stack<Integer> stack;//普通栈
    Stack<Integer> stackMin;//最小栈
    public MinStack() {
        stack = new Stack<>();
        stackMin = new Stack<>();
    }
    //压栈
    public void push(int val) {
        //给最小站赋初值
        if(stackMin.empty()){
            stackMin.push(val);
        }
        //若不是初值，先判断是否小于等于（等于是以防pop后为空）最小栈栈顶元素，若是，才给最小栈压栈
        else if(stackMin.peek() >= val){
                stackMin.push(val);
        }
        stack.push(val);
    }
    
    public void pop() {
        if((int)stackMin.peek() == (int)stack.peek()){
            stackMin.pop();
        }
        stack.pop();
    }
    
    public int top() {
        return stack.peek();
    }
    
    public int getMin() {
        return stackMin.peek();
    }
}